<template>
  <div v-show="$route.name == 'material-save'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/material-list' }">原料列表</el-breadcrumb-item>
      <el-breadcrumb-item>原料采购</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <div style="margin-bottom: 20px;">原料采购</div>
      <el-form :model="material" :rules="rules" ref="material" label-width="100px" class="demo-saveForm">
        <el-row>
          <el-col :span="10">
            <el-form-item label="分类" prop="category_id">
              <el-select v-model="material.category_id" popper-class="dropdownbox" filterable style="width:200px" placeholder="请选择品类" @change="changeCategory">
                <el-option v-for="item in categoryList" :label="item.category_name"
                  :value="item.id" :key="item.id">
                  <span style="float: left">{{ item.category_name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="商品" prop="goods_id">
              <el-select v-model="material.goods_id" popper-class="dropdownbox" filterable style="width:200px" placeholder="请选择商品">
                <el-option v-for="item in goodsList" :label="item.name"
                  :value="item.id" :key="item.id">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="入库单位" prop="unit_in">
              <el-input v-model="material.unit_in" style="width:200px" disabled></el-input>
            </el-form-item>
            <el-form-item label="是否可销售" prop="sellable">
              <el-radio-group v-model="material.sellable">
                <el-radio :label="1" disabled>是</el-radio>
                <el-radio :label="0" disabled>否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="comment">
              <el-input v-model="material.comment" style="width:300px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="入库价格" prop="price_in">
              <el-input-number controls-position="right" :precision="2" :step="1" v-model="material.price_in" placeholder="请输入价格" style="width:200px"></el-input-number>
            </el-form-item>
            <el-form-item label="入库数量" prop="count_in">
              <el-input-number controls-position="right" :precision="2" :step="1" v-model="material.count_in" style="width:200px"></el-input-number>
            </el-form-item>
            <el-form-item label="总价" prop="total_amount">
              <el-input-number controls-position="right" :precision="2" :step="1" v-model="material.total_amount" style="width:200px" disabled></el-input-number>
            </el-form-item>
            <el-form-item label="供应商" prop="supplier">
              <el-input v-model="material.supplier" style="width:200px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-button type="primary" @click="handleSubmit">提交</el-button>
            <el-button  @click="back" style="margin-left: 5px">返回</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      categoryList: [],
      goodsList: [],
      material: {
        price_in: 0.00,
        unit_in: '',
        count_in: 1,
        total_amount: 0.00,
        goods_id: '',
        category_id: '',
        sellable: 1,
        comment: '',
        supplier: ''
      },
      rules: {
        category_id: [
          { required: true, type: 'number', message: '请选择分类', trigger: 'change' }
        ],
        goods_id: [
          { type: 'number', required: true, message: '请选择商品', trigger: 'change' }
        ],
        price_in: [
          { type: 'number', required: true, message: '请输入价格', trigger: 'blur' }
        ],
        count_in: [
          { type: 'number', required: true, message: '请输入数量', trigger: 'blur' }
        ]
      },
      id: ''
    }
  },
  mounted() {
    this.getCategoryList()
    this.getHouseInfo()
  },
  watch: {
    'material.goods_id': {
      handler: function(val, oldval) {
        // 获取商品
        if (val) {
          this.getGoods(val)
        } else {
          this.material.unit_in = ''
        }
      }
    },
    'material.price_in': {
      handler: function(val, oldval) {
        this.material.total_amount = val * this.material.count_in
      }
    },
    'material.count_in': {
      handler: function(val, oldval) {
        this.material.total_amount = val * this.material.price_in
      }
    }
  },
  methods: {
    // 获取id信息
    async getHouseInfo() {
      if (this.$route.params.id) {
        this.id = this.$route.params.id
        localStorage.setItem('materialAddId', this.$route.params.id)
      } else {
        this.id = localStorage.getItem('materialAddId')
      }
      if (this.id) {
        this.getHouse(this.id)
      }
    },
    // 获取分类
    async getCategoryList() {
      const { data: result } = await this.$http.get('/category')
      if (result.status !== 200) return this.$message.error(result.msg)
      this.categoryList = result.data
    },
    // 获取分类下全部商品
    async getGoodsList(flag) {
      if (this.material.category_id) {
        const params = {
          page: 1,
          pageSize: 100000,
          category_id: this.material.category_id,
          process: 0,
        }
        const { data: result } = await this.$http.get('/goods/list', { params: params })
        if (result.status === 200) {
          this.goodsList = result.data
          if (!flag) {
            this.material.goods_id = ''
          } else {
            this.getGoods(this.material.goods_id)
          }
        }
      } else {
        this.material.goods_id = ''
      }
    },
    // 获取原料信息
    async getHouse (id) {
      const { data: result } = await this.$http.get('/house/' + id)
      if (result.status !== 200) {
        return this.$message.error(result.msg)
      }
      this.material.category_id = result.data.category_id
      this.material.goods_id = result.data.goods_id
      this.material.sellable = result.data.sellable
      this.material.unit_in = result.data.unit_in
      this.getGoodsList(1)
    },
    // 更改分类
    changeCategory() {
      this.getGoodsList()
    },
    // 获取商品信息
    async getGoods(id) {
      const { data: result } = await this.$http.get('/goods/' + id)
      if (result.status !== 200) {
        return this.$message.error(result.msg)
      }
      this.material.price_in = result.data.price_in * 1
      this.material.unit_in = result.data.unit_in
      this.material.sellable = result.data.sellable
    },
    // 返回
    back () {
      localStorage.removeItem('materialAddId')
      this.$router.push({'name': 'material-list'})
    },
    // 提交
    handleSubmit() {
      this.$refs.material.validate(async (valid) => {
        if (!valid) return
        let data = {
          goods_id: this.material.goods_id,
          price_in: this.material.price_in,
          count_in: this.material.count_in,
          supplier: this.material.supplier,
          comment: this.material.comment,
          process: 0
        }
        // 可以发起新增入库单的网络请求
        const { data: result } = await this.$http.post('/house', data)
        if (result.status !== 200) {
          return this.$message.error(result.msg)
        }
        this.$message.success('保存成功！')
        this.$router.push({ path: '/material-list' })
      })
    },
  }
}
</script>
<style lang="less">
  .title {
    font-size:20px;
    font-weight: bold
  }
  .marginLeft15 {
    margin-left: 15px
  }
</style>